package codingforgreat.lchot150;

import java.util.PriorityQueue;

public class Lc023 {
      public class ListNode {
     public int val;
     ListNode next;
     ListNode() {}
     ListNode(int val) { this.val = val; }
     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 }
     public ListNode mergeKLists(ListNode[] lists) {
          PriorityQueue<ListNode> queue = new PriorityQueue<>((a,b) -> (a.val - b.val));
          ListNode dummy = new ListNode(-1),p = dummy;
          for(ListNode list : lists){
               if(list != null){
                    queue.add(list);
               }
          }
          while(!queue.isEmpty()){
               ListNode cur = queue.poll();
               p.next = cur;
               if(cur.next != null){
                    queue.add(cur.next);
               }
               p = p.next;
          }
          return dummy.next;
     }
}
